<template>
  <div>
    <el-row>
      <el-select v-model="zoneId" value-key="type" placeholder="请选择主题域" @change="changeZone"
                 style="width: 100%">
        <el-option
          v-for="zone in zoneList"
          :key="zone.id"
          :label="zone.zoneTitle"
          :value="zone.id">
        </el-option>
      </el-select>
      <select-tree
        v-model="themeSetId"
        :options="setList"
        :props="defaultProps"
        placeholder="请选择主题集"
        @change="handleSetChange"
      />
      <el-table
        ref="quotaTable"
        v-loading="loading"
        :data="quotaList"
        row-key="id"
        highlight-current-row
        border
        :show-header="showHeader"
        @current-change="handleQuotaChange">
        <el-table-column prop="name" :show-overflow-tooltip="true"/>
      </el-table>
      <pagination
        small
        layout="prev, pager, next"
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
      />

    </el-row>

  </div>
</template>

<script>
  import { listZone } from '@/api/quota/theme_zone/zone'
  import { getThemeSetTree } from '@/api/quota/theme_zone/set'
  import { listQuota } from '@/api/quota/define/define'
  import SelectTree from '@/components/Widgets/SelectTree'

  export default {
    name: 'QuotaList',
    components: { SelectTree },
    watch:{
      themeSetId(val, oldVal) {
        this.queryParams.themeSetId = val
        if (val !== oldVal)
          this.handleSetChange(val)
      },
    },
    data() {
      return {
        loading: false,
        showHeader: false,
        total: 0,
        size: 12,
        current: 1,
        zoneList: [],
        setList: [],
        quotaList: [],
        zoneId: undefined,
        themeSetId: undefined,
        defaultProps: {
          parent: 'parentId',   // 父级唯一标识
          value: 'id',          // 唯一标识
          label: 'setTitle',       // 标签显示
          children: 'children' // 子级
        },
        queryParams: {
          current: 1,
          size: 15,
          themeSetId: undefined,
          type: '1'
        }
      }
    },
    created() {
      listZone({ pageNum: 1, pageSize: 10000 }).then((response) => {
        if (response.code === 0) {
          this.zoneList = response.data.list
        }
      })
    },
    methods: {
      changeZone(val) {
        this.setList = []
        getThemeSetTree(val).then((response) => {
          this.setList = response.data
        }).catch(() => {
          this.msgSuccess('暂无数据')
        })
      },
      handleQuotaChange(row) {
        this.$emit('change', row)
      },
      handleSetChange(val){
        this.getList()
      },
      getList() {
        this.loading = true
        listQuota(this.queryParams).then(response => {
          this.quotaList = response.data.records
          this.total = response.data.total
          this.loading = false

        })
      },
    }
  }
</script>

<style scoped>

</style>
